﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Input;
using System.Linq;
using System.Text;
using MachineQA.DataAccess;
using System.Data.Objects.DataClasses;
using MachineQA.Properties;
using MachineQA.Model;
using System.Collections.ObjectModel;

namespace MachineQA.ViewModel
{
    /// <summary>
    /// UI-friendly wrapper arounda  Location object.
    /// </summary>
    public class LocationViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        readonly Location _location;
        readonly LocationRepository _locationRepository;
        bool _isSelected = false;
        RelayCommand _saveCommand;

        public LocationViewModel(Location location, LocationRepository locationRepository)
        {
            if (location == null)
                throw new ArgumentNullException("location");

            if (locationRepository == null)
                throw new ArgumentNullException("locationRepository");

            _location = location;
            _locationRepository = locationRepository;
        }

        #region Presentation properties

        public override string DisplayName
        {
            get
            {
                return Resources.LocationViewmodel_DisplayName;
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged(Resources.IsSelectedPropertyName);
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        param => this.Save(),
                        param => this.CanSave
                        );
                }
                return _saveCommand;
            }
        }

        #endregion // Presentation properties

        public string Name
        {
            get { return _location.Name != null ? _location.Name : ""; }
            set
            {
                if (value == _location.Name)
                    return;

                _location.Name = value;
                base.OnPropertyChanged("Name");
            }
        }

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return (_location as IDataErrorInfo).Error; }
        }

        public string this[string propertyName]
        {
            get
            {
                string error = null;

                if (propertyName == "Name")
                    error = this.ValidateName();

                return error;
            }
        }

        string ValidateName()
        {
            if (string.IsNullOrEmpty(this.Name))
                return Resources.LocationViewmodel_Name_Not_Set;

            if (_locationRepository.GetLocations().Where(
                (i) => i.Name == _location.Name && i.Id != _location.Id).Count() > 0)
                return Resources.LocationViewmodel_Duplication_Location;

            return null;
        }

        #endregion // IDataErrorInfo Implementation

        #region Public Methods

        public void Save()
        {
            if (!this.CanSave)
                throw new InvalidOperationException();

            if (this.IsNewLocation)
                _locationRepository.AddLocation(_location);
                
            base.OnPropertyChanged("DisplayName");
        }

        #endregion // Public Methods

#region Helper Functions

        bool IsNewLocation
        {
            get { return !_locationRepository.ContainsLocation(_location); }
        }

        public bool CanSave
        {
            get { return _location.IsValid; }
        }

#endregion // Helper Functions
    }
}
